package com.xzit.rental.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xzit.rental.entity.AutoInfo;
import com.xzit.rental.service.IAutoInfoService;
import com.xzit.rental.utils.Result;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 何时说晚安
 * @since 2024-12-05
 */
@RestController
@RequestMapping("/rental/autoInfo")
public class AutoInfoController {

    @Resource
    private IAutoInfoService autoInfoService;
    @Value("${auto.info.maintain-mileage}")
    private Integer maintainMileage;

    @PostMapping("{start}/{size}")
    public Result search(@PathVariable int start,
                         @PathVariable int size,
                         @RequestBody AutoInfo autoInfo) {
        Page<AutoInfo> page=new Page<>(start,size);
        return Result.success(autoInfoService.searchPage(page,autoInfo));
    }
    @PostMapping
    public Result save(@RequestBody AutoInfo autoInfo) {
        // 设置应保次数与实保次数
        int i = autoInfo.getMileage()/maintainMileage;
        autoInfo.setExpectedNum(i);
        autoInfo.setActualNum(i);
        return autoInfoService.save(autoInfo)?Result.success():Result.fail();
    }
    @PutMapping
    public Result update(@RequestBody AutoInfo autoInfo) {
        // 设置应保次数与实保次数
        int i = autoInfo.getMileage()/maintainMileage;
        autoInfo.setExpectedNum(i);
        autoInfo.setActualNum(i);
        return autoInfoService.updateById(autoInfo)?Result.success():Result.fail();
    }
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable String ids) {
        return autoInfoService.delete(ids)?Result.success():Result.fail();
    }

    @PostMapping("exist")
    public Result exist(@RequestBody AutoInfo autoInfo) {
        AutoInfo info = autoInfoService.selectByAutoNum(autoInfo.getAutoNum());
        return ObjectUtil.isNotEmpty(info)?Result.success().setMessage("have"):Result.success().setMessage("none");
    }

    @GetMapping("toBeMaintain")
    public Result toBeMaintain() {
        return Result.success(autoInfoService.toBeMaintain());
    }

}
